var express = require('express');
var router = express.Router();
var Articles = require('../models/articles');

/* POST Articles listing. */
router.post('/', function(req, res, next) {
      // 获取用户提交的信息
    var postData = {
        author: req.body.author,
        all:req.body.all,
        page: 0,
        count: 100
    };

    if(req.body.page){
        postData.page = parseInt(req.body.page)
    }
    if(req.body.count){
        postData.count = parseInt(req.body.count)
    }
    if(req.body.all){
        Articles.find({})
        .skip(postData.page * postData.count)
        .limit(postData.count)
        .sort({'_id':-1})   //1为从前到后排序    -1为从后到前排序
        .exec(function (err, data) {
            if (data) {//查询所有文章信息
                res.send({
                    state:1,
                    data:data
                });
            }else{
                res.send({
                    state:0,
                    data:[],
                    msg:'未查询到'
                });
            }
        });
    }else{
        Articles.find({author: postData.author})
        .skip(req.body.page * postData.count)
        .limit(postData.count)
        .sort({'_id':-1})
        .exec(function (err, data) {
            if (data) {//查询所有文章信息
                res.send({
                    state:1,
                    data:data
                });
            }else{
                res.send({
                    state:0,
                    data:[],
                    msg:'未查询到'
                });
            }
        });
    }
     

});

/* GET users listing. */
router.get('/', function(req, res, next) {
		var ArticleList = Articles.find({}, function (err, data) {
        if (err) throw  err;
        res.send(data)
    });
})
module.exports = router;
